home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 9404 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.4 KB  |  170 lines

  1. Path: news.unt.edu!news
  2. From: Steve Fogoros <sfogoros@hsc.unt.edu>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: HELP!
  5. Date: Sun, 10 Mar 1996 03:37:44 -0800
  6. Organization: University of North Texas Health Science Center
  7. Message-ID: <3142BF08.1F5C@hsc.unt.edu>
  8. References: <4htsjm$v5o@lantana.singnet.com.sg>
  9. NNTP-Posting-Host: sfogoros.hsc.unt.edu
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0 (Win16; I)
  14.  
  15. Teddy Bear wrote:
  16. > Can anyone please help me my giving me a sample programme?
  17. > I need to write a program that will enter and record an individual's
  18. > details
  19. > search (by name) and display an individual's details
  20. > Delete an individual's details
  21. > Save and restore the entire list to/from a disk file
  22. > Emergency!
  23. > Thanks
  24. > my e-mail add is at s7700038@singnet.com.sg
  25.  
  26. Here is a most basic sample program:
  27.  
  28. #include "stdio.h"
  29. #include "stdlib.h"
  30. #include "string.h"
  31.  
  32. void enter(void);
  33. void show(void);
  34. void delete(void);
  35.  
  36. FILE *fi, *fo;
  37.  
  38. void main(void)
  39. {
  40.    printf("1. enter a record\n");
  41.    printf("2. show a record\n");
  42.    printf("3. delete a record\n");
  43.    printf("4. exit\n");
  44.    printf("Press 1, 2, 3, or 4 then enter\n");
  45.  
  46.    switch((char)getc(stdin))
  47.    {
  48.    case '1': getc(stdin); enter(); break;
  49.    case '2': getc(stdin); show(); break;
  50.    case '3': getc(stdin); delete(); break;
  51.    case '4': exit(0); break;
  52.    default: printf("bad selection. program terminated\n"); exit(0); break;
  53.    }
  54. }
  55.  
  56. void enter(void)
  57. {
  58.    char buff[80];
  59.  
  60.    printf("Enter data followed by return key\n");
  61.    fgets(buff,79,stdin);
  62.  
  63.    fi = fopen("dfile","a");
  64.    if(fi == 0)
  65.    {
  66.       printf("unable to open dfile. program terminated\n");
  67.       exit(0);
  68.    }
  69.  
  70.    fprintf(fi,"%s",buff);
  71.  
  72.    fclose(fi);
  73. }
  74.  
  75. void show(void)
  76. {
  77.    char srch[80], buff[80];
  78.  
  79.    printf("Enter search string and enter\n");
  80.    fgets(srch,79,stdin);
  81.    srch[strlen(srch)-1] = 0;
  82.    printf("\n");
  83.  
  84.    fi = fopen("dfile","r");
  85.    if(fi == 0)
  86.    {
  87.       printf("unable to open dfile. program terminated\n");
  88.       exit(0);
  89.    }
  90.  
  91.    while(!feof(fi))
  92.    {
  93.       fgets(buff,79,fi);
  94.       if(feof(fi)) break;
  95.       if(strstr(buff,srch)) printf("%s",buff);
  96.    }
  97.  
  98.    fclose(fi);
  99. }
  100.  
  101. void delete(void)
  102. {
  103.    char srch[80], buff[80];
  104.  
  105.    printf("Enter search string for delete then enter\n");
  106.    fgets(srch,79,stdin);
  107.    srch[strlen(srch)-1] = 0;
  108.  
  109.    fi = fopen("dfile","r");
  110.    if(fi == 0)
  111.    {
  112.       printf("unable to open dfile. program terminated\n");
  113.       exit(0);
  114.    }
  115.  
  116.    fo = fopen("dfilf","w");
  117.    if(fo == 0)
  118.    {
  119.       printf("unable to open dfilf. program terminated\n");
  120.       exit(0);
  121.    }
  122.  
  123.    while(!feof(fi))
  124.    {
  125.       fgets(buff,79,fi);
  126.       if(feof(fi)) break;
  127.       if(strstr(buff,srch))
  128.       {
  129.          printf("delete this record? press y then enter if yes\n%s",buff);
  130.          if((char)getc(stdin) == 'y')
  131.             continue;
  132.       }
  133.       fprintf(fo,"%s",buff);
  134.    }
  135.  
  136.    fclose(fo);
  137.    fclose(fi);
  138.  
  139.    fi = fopen("dfilf","r");
  140.    if(fi == 0)
  141.    {
  142.       printf("unable to open dfilf. program terminated\n");
  143.       exit(0);
  144.    }
  145.  
  146.    fo = fopen("dfile","w");
  147.    if(fo == 0)
  148.    {
  149.       printf("unable to open dfile. program terminated\n");
  150.       exit(0);
  151.    }
  152.  
  153.    while(!feof(fi))
  154.    {
  155.       fgets(buff,79,fi);
  156.       if(feof(fi)) break;
  157.       fprintf(fo,"%s",buff);
  158.    }
  159.  
  160.    fclose(fo);
  161.    fclose(fi);
  162. }
  163.  
  164. -- 
  165. Steve Fogoros, Academic Information Coordinator
  166. University of North Texas Health Science Center
  167. sfogoros@hsc.unt.edu
  168.